#include <iostream>
#include <cstring>
#include "User.h"

using namespace std;
using namespace LB;


User::User(int m_uid,const char* m_username,const char* m_password,float balance)
{
	this->m_uid = m_uid;
	strcpy(this->m_username,m_username);
	strcpy(this->m_password,m_password);
	this->balance = balance;
}

User::User(int m_uid,const char* m_username,const char* m_password,float balance,map<int,char*> v_usrmap)
{
	this->m_uid = m_uid;
	strcpy(this->m_username,m_username);
	strcpy(this->m_password,m_password);
	this->balance = balance;
	mybook = v_usrmap;
}

User& User::operator = (const User &that)
{
	this->m_uid = that.m_uid;
	strcpy(this->m_username,that.m_username);
	strcpy(this->m_password,that.m_password);
	this->balance = that.balance;
	this->mybook = that.mybook;
	return *this;
}

bool User::borrowbook(int book_id,char* bookname)
{
	if (mybook.size() != 5)
	{
		mybook.insert(make_pair(book_id,bookname));
		return true;
	}
	else
	{
		return false;
	}
}

bool User::revertbook(int bookid)
{
	if (mybook.end() != mybook.find(bookid))//find fail return end iterator
	{
		mybook.erase(bookid);
		return true;
	}
	else
	{
		return false;
	}
	
}

void User::listmyborrow(void)
{
	map<int,char*>::iterator p = mybook.begin();
	cout << "所借书籍:" << endl;
	while(p != mybook.end())
	{
		cout << "书号: " << p->first 
		     << " 书名: " << p->second << endl;
		p++;
	}
}

bool User::operator == (const User &that)
{
	return this->m_uid == that.m_uid;
}

User::~User(void)
{
	
}

namespace LB
{
	ostream& operator << (ostream& os,User& that)
	{
		cout << "用户id:" << that.m_uid << endl;
		cout << "用户名:" << that.m_username << endl;
		cout << "余额:" << that.balance << endl;
		that.listmyborrow();
		return os;
	}
}
